﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Commands;
namespace Web.Cms.UserControls
{
    public partial class TabNav : AdminUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int _contentId = 0;
            _contentId = ConvertUtility.ToInt32(Request.QueryString.Get("distid"));
            if (!IsPostBack)
            {
                if (CurrentCommand != null)
                {
                    List<CommandInfo> lstSubCommands = CommandController.GetCommands(CurrentPortalID, CurrentCommand.ParentID);
                    List<CommandInfo> lstAvailableCommands = new List<CommandInfo>();
                    foreach (CommandInfo cmd in lstSubCommands)
                    {
                        if (!cmd.IsEnable || !cmd.IsVisible || !CurrentUser.IsSuperUser && !CommandController.UserHasPermision(CurrentUser.UserID, cmd.CommandID)) continue;
                        lstAvailableCommands.Add(cmd);
                    }
                    rptMenu.DataSource = lstAvailableCommands;
                    rptMenu.ItemDataBound += new RepeaterItemEventHandler(rptMenu_ItemDataBound);
                    rptMenu.DataBind();
                }
            }
        }
        protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                CommandInfo cmd = (CommandInfo)e.Item.DataItem;
                HyperLink lnkCmd = (HyperLink)e.Item.FindControl("lnkCmd");
                Literal litStyle = (Literal)e.Item.FindControl("litStyle");
                string tabkey = CurrentCommand.CtrlKey;
                if (Request.QueryString.Get("tabkey") != null)
                    tabkey = Request.QueryString.Get("tabkey");
                if (cmd.CtrlKey.ToLower().Trim() == tabkey.ToLower().Trim())
                    litStyle.Text = "Tab";
                else
                {
                    litStyle.Text = "Tabvisited";
                }
                if (cmd.CtrlKey != string.Empty)
                    lnkCmd.NavigateUrl = UIProcess.GetCPPage(CurrentPortalID, cmd.CtrlKey, null, null);
                lnkCmd.Text = cmd.Title;
            }
        }      
    }
}